package com.cooper.dao.account;

import java.util.List;

import com.cooper.constants.ResourceConstants;
import com.cooper.entity.ShopCartEntity;
import com.cooper.pagination.PageInfo;
import com.cooper.vo.CompositeShopCartItemVo;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.Insert;
import net.paoding.rose.jade.annotation.Paged;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.Update;

@DAO
public interface ShopCartDAO {

	@Insert(tableName="d_shop_cart")
	Long addShopCart(ShopCartEntity shopCart);
	
	@Update(tableName="d_shop_cart", keyColumn="id")
	void updateShopCart(ShopCartEntity shopCart);
	
	@SQL("update d_shop_cart set user_id=:1 where cookie=:2 and (user_id is null or user_id='') and deleted='n'")
	public void updateUserIdByCookie(Long userId , String cookie);
	
	@SQL("update   d_shop_cart  set deleted='y' where id=:1")
	void deleteShopCart(Long id);
	
	@SQL("update   d_shop_cart  set deleted='y'  where id in(:1)")
	void deleteShopCart(List<Long> ids);
	
	@SQL("select * from  d_shop_cart where id=:1")
	ShopCartEntity getShopCartDo(Long id);
	
	@Paged(paramIndex=1)
	@SQL("select a.*,b.title,b.cn_name,(s.price-b.discount_volume) as price,c.origin_url as display_file_id,"
			+ " (select max(d.country_name) from d_address d where d.deleted='n' and d.origin='" + ResourceConstants.AddressOrigin.ITEM_ORIGIN + "' and d.outer_id=b.id) item_origin "
			+ " from d_shop_cart a, d_item_info b, d_item_sku s, d_item_image c"
			+ " where a.deleted='n' and b.deleted='n' and c.deleted='n'"
			+ " and a.item_id=b.id and b.id=s.item_id and b.id=c.item_id"
			+ " and c.sort_no=1 and a.sku_id = s.id" 
			+ " #if(null !=:1.userId && ''!=:1.userId){ and a.user_id=:1.userId} "
			+ " #if(null !=:1.itemId && ''!=:1.itemId){ and a.item_id=:1.itemId} "
			+ " #if(null !=:1.skuId && ''!=:1.skuId){ and s.id =:1.skuId} "
			+ " #if(null !=:1.cookie && ''!=:1.cookie){ and a.cookie=:1.cookie} ")
	List<CompositeShopCartItemVo> findCompositeShopCartItemVo(CompositeShopCartItemVo queryParamVo, PageInfo pageInfo);
	
	@SQL("select s.item_id,s.id sku_id,s.properties,b.title,b.cn_name,(s.price-b.discount_volume) as price,c.origin_url as display_file_id,"
			+ "(select max(d.country_name) from d_address d where d.deleted='n' and d.origin='" + ResourceConstants.AddressOrigin.ITEM_ORIGIN + "' and d.outer_id=b.id) item_origin "
			+ " from d_item_info b, d_item_sku s, d_item_image c"
			+ " where  b.deleted='n' and c.deleted='n' and b.id=s.item_id and b.id=c.item_id and c.sort_no=1"
			+ " and b.id=:1 and s.id=:2 ")
	CompositeShopCartItemVo getCompositeShopCart(long itemId, long skuId) throws Exception;
	
	@SQL("select count(*) from d_shop_cart a, d_item_info b, d_item_sku s, d_item_image c"
			+ " where a.deleted='n' and b.deleted='n' and c.deleted='n'"
			+ " and a.item_id=b.id and b.id=s.item_id and b.id=c.item_id"
			+ " and c.sort_no=1 and a.sku_id = s.id" 
			+ " #if(null !=:1.userId && ''!=:1.userId){ and a.user_id=:1.userId} "
			+ " #if(null !=:1.skuId && ''!=:1.skuId){ and s.id =:1.skuId} "
			+ " #if(null !=:1.itemId && ''!=:1.itemId){ and a.item_id=:1.itemId} "
			+ " #if(null !=:1.cookie && ''!=:1.cookie){ and a.cookie=:1.cookie} ")
	int findCompositeShopCartItemVoCount(CompositeShopCartItemVo queryParamVo);
}
